/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package coalescent.cef.command.listener;

import cef.command.CommandI;
import cef.command.listener.CommandOutputEvent;
import cef.command.listener.CommandOutputListener;
import cef.command.listener.CommandOutputListenerException;
import cef.command.listener.FigureEditorOutputComponent;
import coalescent.cef.command.MLE_KC64ThetaCommand;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import java.math.BigDecimal;
import java.util.Map;

/**
 * @version version
 * @user Susanta Tewari
 * @history First created on 10/31/12.
 * @since 1.4.0
 */
public class MLE_KC64ThetaOutputComponent extends FigureEditorOutputComponent {

    @Override
    public void initComponents(final CommandOutputEvent event)
            throws CommandOutputListenerException {

        final MLE_KC64ThetaCommand command = (MLE_KC64ThetaCommand) event.getSource();

        title = "Mutation Rate MLE";

        final XYSeries xyseries = new XYSeries(title);


        // update data
        final Map<Double, BigDecimal> theta_probs = command.getThetaProbabilities();

        for (final double theta : theta_probs.keySet()) {
            xyseries.add(theta, theta_probs.get(theta));
        }

        final NumberAxis x_axis = new NumberAxis("Theta");
        final NumberAxis y_axis = new NumberAxis("Probability");

        x_axis.setAutoRangeIncludesZero(false);
        y_axis.setAutoRangeIncludesZero(false);

        final XYSplineRenderer xysplinerenderer = new XYSplineRenderer();
        final XYPlot xyplot = new XYPlot(new XYSeriesCollection(xyseries), x_axis, y_axis,
                                         xysplinerenderer);

        /* chart */
        final JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, xyplot, true);

        figure = new ChartPanel(chart);
    }

    @Override
    public CommandOutputListener newInstance() {
        return new MLE_KC64ThetaOutputComponent();
    }

    @Override
    public Class<? extends CommandI> getTargetCommandClass() {
        return MLE_KC64ThetaCommand.class;
    }
}
